# coding=utf-8
import requests
import redis
from django.conf import settings

password = settings.REDIS_PASSWORD

class SceRedis():

    def __init__(self):
        self.redis_master = redis.Redis(
            connection_pool=redis.ConnectionPool(
                **self.get_redis_conf(master=1)))
        self.redis_slave = redis.Redis(
            connection_pool=redis.ConnectionPool(
                **self.get_redis_conf(master=0)))

    @staticmethod
    def instance():
        if not hasattr(SceRedis, "_instance"):
            SceRedis._instance = SceRedis()
        try:
            SceRedis._instance.redis_master.ping()
        except:
            SceRedis._instance = SceRedis()
        return SceRedis._instance

    def get_redis_conf(self, master=1):
        redis_config = {
            "host": "r-2ze2b3f432ea2f04.redis.rds.aliyuncs.com",
            "port": 6379,
            "db": 1,
            "password": "aTN32W8Wpua743ZE",
            'socket_timeout': None,
            'encoding': 'utf-8',
            'encoding_errors': 'strict',
            'decode_responses': True
        }
        return redis_config

